package com.atguigu.gulimall.finance.controller;

import cn.hutool.core.util.StrUtil;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;
import com.atguigu.gulimall.finance.entity.MainEntity;
import com.atguigu.gulimall.finance.entity.TypeEntity;
import com.atguigu.gulimall.finance.entity.vo.ExcelFinanceVo;
import com.atguigu.gulimall.finance.entity.vo.FinanceVo;
import com.atguigu.gulimall.finance.entity.vo.MainEntityVo;
import com.atguigu.gulimall.finance.entity.vo.MainPipDataVo;
import com.atguigu.gulimall.finance.service.MainService;
import com.atguigu.gulimall.finance.service.TypeService;
import com.atguigu.gulimall.finance.utils.ExcelUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 财务收入
 *
 * @author huguangwei
 * @email huguangwei@gmail.com
 * @date 2023-01-19 09:18:37
 */
@RestController
@RequestMapping("finance/main")
public class MainController {
    @Autowired
    private MainService mainService;

    @Autowired
    private TypeService typeService;

    /**
     * 获取总财务
     */
    @GetMapping("/sumfinance")
    public R getSumFinance() {
        FinanceVo financeVo = mainService.getSumFinance();
        return R.ok().put("data", financeVo);
    }

    /**
     * 导出Excel
     */
    @GetMapping("/exportExcel")
    public void export(HttpServletResponse response) {
        List<ExcelFinanceVo> allFinalce = getAllFinalcce();
        ExcelUtil.exportExcel(allFinalce, "财务数据信息", "财务数据信息"
                , ExcelFinanceVo.class, "财务数据信息.xls", response);
    }

    @GetMapping("/getAllFinalce")
    public List<ExcelFinanceVo> getAllFinalcce() {
        return mainService.getAllFinalce();
    }

    /**
     * 获取所有类型
     *
     * @return List<TypeEntity>
     */
    @GetMapping("/typeList")
    public R getTypeList() {
        List<TypeEntity> list = typeService.list();
        return R.ok().put("data", list);
    }

    /**
     * 根据名称获取类型值
     * /finance/main/getTypeValueByName/${this.value}
     *
     * @return
     */
    @GetMapping("/getTypeValueByName/{value}")
    public R getTypeValueByName(@PathVariable("value") String value) {

        if (StrUtil.isNotEmpty(value)) {
            TypeEntity type = typeService.getOne(new QueryWrapper<TypeEntity>().eq("type_name", value));
            return R.ok().put("data", type.getTypeValue().toString());
        }
        return R.error();
    }

    /**
     * 统计饼状图-支出
     *
     * @return List<MainPipDataVo>
     */
    @GetMapping("/piplist/expend")
    public R getPipData() {
        List<MainPipDataVo> pipDataVos = mainService.getExpendSumMoney();
        return R.ok().put("data", pipDataVos);
    }

    /**
     * 统计饼状图-收入
     *
     * @return
     */
    @GetMapping("/piplist/incom")
    public R getPipDataIncom() {
        List<MainPipDataVo> pipDataVos = mainService.getSumMoneyIcome();
        if (pipDataVos != null && pipDataVos.size() > 0) {
            return R.ok().put("data", pipDataVos);
        }
        return R.ok().put("data", null);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = mainService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 获取单条信息
     *
     * @param id 编号
     * @return MainEntityVo
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        MainEntityVo mainEntityVo = mainService.getInfo(id);
        return R.ok().put("income", mainEntityVo);
    }

    /**
     * 保存
     *
     * @param income MainEntity实体
     * @return R
     */
    @RequestMapping("/save")
    public R save(@RequestBody MainEntity income) {
        mainService.saveMain(income);
        return R.ok();
    }

    /**
     * 修改
     *
     * @param income MainEntity实体
     * @return R
     */
    @RequestMapping("/update")
    public R update(@RequestBody MainEntity income) {
        mainService.updateIcomeById(income);

        return R.ok();
    }

    /**
     * 删除
     *
     * @param ids 编号集合
     * @return R
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        mainService.removeIcomeByIds(Arrays.asList(ids));
        return R.ok();
    }

}
